package com.turing.post.datatranfer.impl;


import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.datatranfer.util.OldSaleBranchDao;
import com.turing.post.datatranfer.util.OldSalebranchBusiness;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchBusiness;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 实现接口
 * 
 * @author WZD
 */
@Stateless(mappedName = "datatranfer.impl.OldSaleBranchBusinessImpl")
@Remote
@Local
public class OldSaleBranchBusinessImpl implements OldSalebranchBusiness {

	
	@EJB(mappedName = "organization.impl.SaleBranchBusinessImpl")
	private SaleBranchBusiness saleBranchBusinessImpl;// 新数据库商户接口
    
	@EJB(mappedName = "datatranfer.impl.OldSaleBanchDaoImpl")
	private OldSaleBranchDao oldSaleBanchDaoImp;// 新数据库商户接口
	
	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;// 对应表接口
	
	public void insertNewSaleBranch(PCPSWebInput webInput1,String issuecompanyno,String regionNo,String issbranchno,String incomeBranchId) throws PCPSApplicationException{
		List<Object[]> list = oldSaleBanchDaoImp.getOldSaleBranchList();
		SaleBranch salebranch = null;
		Object[] obj = null;
		PCPSWebInput webInput=new PCPSWebInput();
		webInput.setOperBranchNo(issuecompanyno);
		webInput.setOperId(new BigInteger("9"));
		
		SaleBranch salebranch1=new SaleBranch();
		String salebranchno1=issuecompanyno+"98"+regionNo+"1000";
		salebranch1.setSaleBranchNo(salebranchno1);
		salebranch1.setSaleBranchName("销售机构");
		salebranch1.setIssueBranchNo(issbranchno);
		salebranch1.setStatus("1");
		salebranch1.setRegionNo(regionNo);
		salebranch1.setIncomeBranchId(new BigInteger(incomeBranchId));
		salebranch1.setLastCash(new BigInteger("0"));
		salebranch1.setCash(new BigInteger("0"));
		salebranch1.setSaleType("1");
		//salebranch1.setAddress(obj[3]==null?"":obj[3].toString());
		//salebranch1.setLinkTele(obj[2]==null?"":obj[2].toString());
		saleBranchBusinessImpl.insertSaleBranchInfo(webInput, salebranch1);
		
		for (int i = 0; i < list.size(); i++) {
			obj = list.get(i);			
			// 检查发卡商机构是否存在
			if (PCPSUtil.isEmpty(issuecompanyno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡商为空!");
			}			
			// 检查行政区域码否存在
			if (PCPSUtil.isEmpty(regionNo)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "行政区域码为空!");
			}
			// 检查卡种类是否存在
			if (PCPSUtil.isEmpty(incomeBranchId)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "收益机构为空!");
			}
			if (PCPSUtil.isEmpty(issbranchno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡机构号为空!");
			}
			
			
			if(PCPSUtil.isEmpty(contrastDaoImpl.getMapSaleBranchByNo(obj[0].toString()))){
				salebranch=new SaleBranch();
				String salebranchno=salebranchno1+String.format("%02d", i);
				salebranch.setSaleBranchNo(salebranchno);
				salebranch.setSaleBranchName(obj[1].toString());
				salebranch.setSuperSaleBranchNo(salebranchno1);
				salebranch.setIssueBranchNo(issbranchno);
				salebranch.setStatus("1");
				salebranch.setRegionNo(regionNo);
				salebranch.setIncomeBranchId(new BigInteger(incomeBranchId));
				salebranch.setLastCash(new BigInteger("0"));
				salebranch.setCash(new BigInteger("0"));
				salebranch.setSaleType("1");
				salebranch.setAddress(obj[3]==null?"":obj[3].toString());
				salebranch.setLinkTele(obj[2]==null?"":obj[2].toString());
				saleBranchBusinessImpl.insertSaleBranchInfo(webInput, salebranch);
				
				contrastDaoImpl.insertMapSaleBranch(obj[0].toString(), salebranchno);
			}
		}
	}
}
